﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace EasyDDD.AdminModule.Application.QueriesSysAdminRole
{
    public record SysAdminRoleGetListQuery : IQuery<List<SysAdminRoleGetListQueryResultItem>>
    {
    }

    [AutoMapperFrom(typeof(Models.ApplicationRole))]
    public record SysAdminRoleGetListQueryResultItem : Models.ApplicationRole
    {
        [JsonIgnore]
        [NotMapped]
        public override bool IsDeleted { get; set; } = default!;
        [JsonIgnore]
        [NotMapped]
        public override Guid TenantId { get; set; } = default!;
    }


    public class SysAdminRoleGetListQueryHandler(
        Records.SysAdminRoleRecords records,
        IMapper mapper,
        ILogger<SysAdminRoleGetListQueryHandler> logger)
        : IQueryHandler<SysAdminRoleGetListQuery, List<SysAdminRoleGetListQueryResultItem>>
    {
        public Task<List<SysAdminRoleGetListQueryResultItem>> Handle(SysAdminRoleGetListQuery request, CancellationToken cancellationToken)
        {
            logger.LogDebug("查询 SysAdminRoleGetListQueryHandler");
            var query = records.AsQueryable();
            query = query.OrderBy(e => e.Id);
            return mapper.ProjectTo<SysAdminRoleGetListQueryResultItem>(query).ToListAsync(cancellationToken);
        }
    }
}
